package test.org.dbstar.scheduler.control;

import java.util.ArrayList;
import java.util.List;

import org.dbstar.scheduler.beans.Catalog;
import org.dbstar.scheduler.beans.CmdJobConf;
import org.dbstar.scheduler.beans.JobConf;
import org.dbstar.scheduler.beans.ParameterDefine;
import org.dbstar.scheduler.beans.ParameterDefineType;
import org.dbstar.scheduler.control.CatalogController;
import org.dbstar.scheduler.control.JobConfController;
import org.dbstar.scheduler.control.cache.CacheableCatalogController;
import org.dbstar.scheduler.control.db.catalog.JdbcTemplateCatalogController;
import org.dbstar.scheduler.control.exception.DaoException;
import org.dbstar.scheduler.identifier.Identifier;
import org.dbstar.scheduler.identifier.IdentifierType;
import org.dbstar.scheduler.job.IllegalFactoryException;

import test.org.dbstar.scheduler.SchedulerTestCase;


/**
 * 测试JobConfController的TestCase基类
 * 
 * @author 代波
 * @since 1.0.0
 * @version 1.0.0
 * @date 2012-4-24
 */
public abstract class JobConfControllerTestCase extends SchedulerTestCase {
	private CatalogController catalogController;
	private JobConfController controller;
	private Catalog root;
	private int rootId;

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		catalogController = new CacheableCatalogController(new JdbcTemplateCatalogController(jt));
		controller = buildJobConfController();
		root = catalogController.getRootCatalog();
		rootId = root.getId();
	}

	@Override
	protected void tearDown() throws Exception {
		controller = null;
		catalogController = null;
		super.tearDown();
	}

	protected abstract JobConfController buildJobConfController() throws DaoException;

	private void checkEquals(JobConf c1, JobConf c2) {
		assertEquals(c1.getIdentifier(), c2.getIdentifier());
		assertEquals(c1.getCatalog(), c2.getCatalog());
		assertEquals(c1.getName(), c2.getName());
		assertEquals(c1.getDescription(), c2.getDescription());
		assertEquals(c1.getType(), c2.getType());
		assertEquals(c1.getContent(), c2.getContent());
		assertEquals(c1.getParameters(), c2.getParameters());
		assertEquals(c1.getCreateTime(), c2.getCreateTime());
		assertEquals(c1.getModifyTime(), c2.getModifyTime());
		assertEquals(c1, c2);
	}

	private JobConf newCmdJobConf(int catalog, String name, String desc, String cmd) {
		CmdJobConf conf = new CmdJobConf();
		conf.setCatalog(catalog);
		conf.setName(name);
		conf.setDescription(desc);
		conf.setCmd(cmd);
		conf.setOwner("bdai");
		return conf;
	}

	private JobConf newCmdJobConf(int catalog, String name, String desc, String cmd, long createTime) {
		JobConf conf = newCmdJobConf(catalog, name, desc, cmd);
		conf.setCreateTime(createTime);
		return conf;
	}

	private Catalog newCataLog(int parent, String name, String desc) {
		Catalog catalog = new Catalog();
		catalog.setParent(parent);
		catalog.setName(name);
		catalog.setDescription(desc);
		catalog.setOwner("bdai");
		return catalog;
	}

	public void testGetAllJobConfs() throws DaoException {
		List<JobConf> list = controller.getAllJobConfs();
		assertEquals(0, list.size());
	}

	public void testGetJobConfs() throws DaoException {
		assertEquals(0, controller.getJobConfs(rootId).size());
	}

	// 测试增加catalog为已存在目录
	public void testAddJobConf() throws DaoException, IllegalFactoryException {
		long ct = System.currentTimeMillis();
		JobConf newConf = newCmdJobConf(rootId, "name", "desc", "ping www.sina.com.cn", ct);
		Identifier identifier = identifierFactory.createIdentifier(IdentifierType.conf);
		newConf.setIdentifier(identifier);
		newConf.setModifyTime(System.currentTimeMillis());

		// 传入对象的Identifier值会被正确设置为自动生成的Identifier，且通过addJobConf方法返回
		assertEquals(controller.addJobConf(newConf), newConf.getIdentifier());

		assertNotNull(newConf.getIdentifier());// Identifier不为null
		assertNotSame(identifier, newConf.getIdentifier());// 传入对象的Identifier值会被覆盖
		assertEquals(newConf.getCatalog(), rootId);
		assertEquals(newConf.getType(), CmdJobConf.JOB_TYPE);
		assertEquals(newConf.getName(), "name");
		assertEquals(newConf.getDescription(), "desc");
		assertEquals(newConf.getContent(), "ping www.sina.com.cn");
		assertNull(newConf.getParameters());
		assertEquals(newConf.getCreateTime(), ct);
		assertNull(newConf.getModifyTime());// 传入对象的最后生成时间会被设置为null

		checkEquals(newConf, controller.getJobConf(newConf.getIdentifier()));

		List<JobConf> list = controller.getAllJobConfs();
		assertEquals(1, list.size());
		checkEquals(newConf, list.get(0));

		list = controller.getJobConfs(rootId);
		assertEquals(1, list.size());
		checkEquals(newConf, list.get(0));
	}

	// 其他允许Null的值
	public void testAddJobConfNullable() throws DaoException {
		JobConf newConf = newCmdJobConf(rootId, "name", null, null);

		// 传入对象的Identifier值会被正确设置为自动生成的Identifier，且通过addJobConf方法返回
		assertEquals(controller.addJobConf(newConf), newConf.getIdentifier());

		assertNotNull(newConf.getIdentifier());// Identifier不为null
		assertEquals(newConf.getCatalog(), rootId);
		assertEquals(newConf.getType(), CmdJobConf.JOB_TYPE);
		assertEquals(newConf.getName(), "name");
		assertNull(newConf.getDescription());
		assertNull(newConf.getContent());
		assertNull(newConf.getParameters());
		assertTrue(newConf.getCreateTime() > 0 && newConf.getCreateTime() <= System.currentTimeMillis());
		assertNull(newConf.getModifyTime());// 传入对象的最后生成时间会被设置为null

		checkEquals(newConf, controller.getJobConf(newConf.getIdentifier()));

		List<JobConf> list = controller.getAllJobConfs();
		assertEquals(1, list.size());
		checkEquals(newConf, list.get(0));

		list = controller.getJobConfs(rootId);
		assertEquals(1, list.size());
		checkEquals(newConf, list.get(0));
	}

	// 有参数的，但参数数量为0
	public void testAddJobConfWithEmptyParameters() throws DaoException {
		JobConf newConf = newCmdJobConf(rootId, "name", null, null);
		newConf.setParameters(new ArrayList<ParameterDefine>());

		// 传入对象的Identifier值会被正确设置为自动生成的Identifier，且通过addJobConf方法返回
		assertEquals(controller.addJobConf(newConf), newConf.getIdentifier());

		assertNotNull(newConf.getIdentifier());// Identifier不为null
		assertEquals(newConf.getCatalog(), rootId);
		assertEquals(newConf.getType(), CmdJobConf.JOB_TYPE);
		assertEquals(newConf.getName(), "name");
		assertNull(newConf.getDescription());
		assertNull(newConf.getContent());
		assertNull(newConf.getParameters());
		assertTrue(newConf.getCreateTime() > 0 && newConf.getCreateTime() <= System.currentTimeMillis());
		assertNull(newConf.getModifyTime());// 传入对象的最后生成时间会被设置为null

		checkEquals(newConf, controller.getJobConf(newConf.getIdentifier()));

		List<JobConf> list = controller.getAllJobConfs();
		assertEquals(1, list.size());
		checkEquals(newConf, list.get(0));

		list = controller.getJobConfs(rootId);
		assertEquals(1, list.size());
		checkEquals(newConf, list.get(0));
	}

	// 有参数的，且参数数量大于0
	public void testAddJobConfWithMoreParameters() throws DaoException {
		JobConf newConf = newCmdJobConf(rootId, "name", null, null);
		List<ParameterDefine> parameters = new ArrayList<ParameterDefine>();
		parameters.add(new ParameterDefine("p1", ParameterDefineType.none, null));
		parameters.add(new ParameterDefine("p2", ParameterDefineType.pre, "abc"));
		parameters.add(new ParameterDefine("p3", ParameterDefineType.lastDay, "yyyy-MM-dd"));
		newConf.setParameters(parameters);

		// 传入对象的Identifier值会被正确设置为自动生成的Identifier，且通过addJobConf方法返回
		assertEquals(controller.addJobConf(newConf), newConf.getIdentifier());

		assertNotNull(newConf.getIdentifier());// Identifier不为null
		assertEquals(newConf.getCatalog(), rootId);
		assertEquals(newConf.getType(), CmdJobConf.JOB_TYPE);
		assertEquals(newConf.getName(), "name");
		assertNull(newConf.getDescription());
		assertNull(newConf.getContent());
		assertEquals(newConf.getParameterSize(), 3);
		assertEquals(newConf.getParameters(), parameters);
		assertTrue(newConf.getCreateTime() > 0 && newConf.getCreateTime() <= System.currentTimeMillis());
		assertNull(newConf.getModifyTime());// 传入对象的最后生成时间会被设置为null

		checkEquals(newConf, controller.getJobConf(newConf.getIdentifier()));

		List<JobConf> list = controller.getAllJobConfs();
		assertEquals(1, list.size());
		checkEquals(newConf, list.get(0));

		list = controller.getJobConfs(rootId);
		assertEquals(1, list.size());
		checkEquals(newConf, list.get(0));
	}

	// 测试增加catalog为不存在目录
	public void testAddJobConfCatalogNotExist() throws DaoException, IllegalFactoryException {
		JobConf newConf = newCmdJobConf(10000, "name", "desc", "ping www.sina.com.cn");
		Identifier identifier = identifierFactory.createIdentifier(IdentifierType.conf);
		newConf.setIdentifier(identifier);
		long mt = System.currentTimeMillis();
		newConf.setModifyTime(mt);

		try {
			controller.addJobConf(newConf);
			fail("应该抛出DaoException");
		} catch (DaoException e) {
			e.printStackTrace();
		}

		// 新增识别的情况下，传入值不会被覆盖
		assertEquals(newConf.getIdentifier(), identifier);
		assertEquals(newConf.getModifyTime().longValue(), mt);

		assertNull(controller.getJobConf(identifier));
		assertEquals(0, controller.getAllJobConfs().size());
		assertEquals(0, controller.getJobConfs(10000).size());
	}

	// name不能为null
	public void testAddJobConfNameNull() throws DaoException {
		JobConf newConf = newCmdJobConf(rootId, null, "desc", "ping www.sina.com.cn");

		try {
			controller.addJobConf(newConf);
			fail("应该抛出DaoException");
		} catch (DaoException e) {
			e.printStackTrace();
		}

		assertNull(newConf.getIdentifier());
		assertEquals(0, controller.getAllJobConfs().size());
		assertEquals(0, controller.getJobConfs(rootId).size());
	}

	// name不能仅仅为空格字符组成
	public void testAddJobConfNameBlank() throws DaoException {
		JobConf newConf = newCmdJobConf(rootId, " \t ", "desc", "ping www.sina.com.cn");

		try {
			controller.addJobConf(newConf);
			fail("应该抛出DaoException");
		} catch (DaoException e) {
			e.printStackTrace();
		}

		assertNull(newConf.getIdentifier());
		assertEquals(0, controller.getAllJobConfs().size());
		assertEquals(0, controller.getJobConfs(rootId).size());
	}

	public void testUpdateJobConf() throws DaoException {
		int c1 = catalogController.addCatalog(newCataLog(rootId, "c1", null));

		long ct = System.currentTimeMillis();
		Identifier identifier = controller.addJobConf(newCmdJobConf(rootId, "name", "desc", "ping", ct));

		JobConf editInfo = controller.getJobConf(identifier);
		editInfo.setCatalog(c1);
		editInfo.setName("name3");
		editInfo.setDescription("desc3");
		((CmdJobConf) editInfo).setCmd("ping2");
		editInfo.setCreateTime(System.currentTimeMillis());
		List<ParameterDefine> parameters = new ArrayList<ParameterDefine>();
		parameters.add(new ParameterDefine("p1", ParameterDefineType.none, null));
		parameters.add(new ParameterDefine("p2", ParameterDefineType.pre, "abc"));
		parameters.add(new ParameterDefine("p3", ParameterDefineType.lastDay, "yyyy-MM-dd"));
		editInfo.setParameters(parameters);
		Long mt = System.currentTimeMillis();
		editInfo.setModifyTime(mt);
		controller.updateJobConf(editInfo);

		JobConf info = controller.getJobConf(identifier);
		assertEquals(identifier, info.getIdentifier());
		assertEquals(info.getCatalog(), c1);
		assertEquals(info.getType(), CmdJobConf.JOB_TYPE);
		assertEquals(info.getName(), "name3");
		assertEquals(info.getDescription(), "desc3");
		assertEquals(info.getContent(), "ping2");
		assertEquals(info.getParameterSize(), 3);
		assertEquals(info.getParameters(), parameters);
		assertEquals(info.getCreateTime(), ct);// 创建时间不变
		assertNotSame(info.getModifyTime(), mt);// 修改时间会被覆盖
		assertTrue(info.getModifyTime() != null && info.getModifyTime().longValue() > 0
				&& info.getModifyTime().longValue() <= System.currentTimeMillis());// 修改时间自动设置

		editInfo.setCreateTime(ct);
		checkEquals(editInfo, info);

		List<JobConf> list = controller.getAllJobConfs();
		assertEquals(1, list.size());
		assertEquals(editInfo, list.get(0));
		assertEquals(0, controller.getJobConfs(rootId).size());
		list = controller.getJobConfs(c1);
		assertEquals(1, list.size());
		assertEquals(editInfo, list.get(0));
	}

	public void testUpdateJobConfWithEmptyParameters() throws DaoException {
		int c1 = catalogController.addCatalog(newCataLog(rootId, "c1", null));

		long ct = System.currentTimeMillis();
		JobConf newConf = newCmdJobConf(rootId, "name", "desc", "ping", ct);
		List<ParameterDefine> parameters = new ArrayList<ParameterDefine>();
		parameters.add(new ParameterDefine("p1", ParameterDefineType.none, null));
		parameters.add(new ParameterDefine("p2", ParameterDefineType.pre, "abc"));
		parameters.add(new ParameterDefine("p3", ParameterDefineType.lastDay, "yyyy-MM-dd"));
		newConf.setParameters(parameters);
		Identifier identifier = controller.addJobConf(newConf);

		JobConf editInfo = controller.getJobConf(identifier);
		editInfo.setCatalog(c1);
		editInfo.setName("name3");
		editInfo.setDescription("desc3");
		((CmdJobConf) editInfo).setCmd("ping2");
		editInfo.setCreateTime(System.currentTimeMillis());
		editInfo.setParameters(new ArrayList<ParameterDefine>());
		Long mt = System.currentTimeMillis();
		editInfo.setModifyTime(mt);
		controller.updateJobConf(editInfo);

		JobConf info = controller.getJobConf(identifier);
		assertEquals(identifier, info.getIdentifier());
		assertEquals(info.getCatalog(), c1);
		assertEquals(info.getType(), CmdJobConf.JOB_TYPE);
		assertEquals(info.getName(), "name3");
		assertEquals(info.getDescription(), "desc3");
		assertEquals(info.getContent(), "ping2");
		assertNull(info.getParameters());
		assertEquals(info.getCreateTime(), ct);// 创建时间不变
		assertNotSame(info.getModifyTime(), mt);// 修改时间会被覆盖
		assertTrue(info.getModifyTime() != null && info.getModifyTime().longValue() > 0
				&& info.getModifyTime().longValue() <= System.currentTimeMillis());// 修改时间自动设置

		editInfo.setCreateTime(ct);
		checkEquals(editInfo, info);

		List<JobConf> list = controller.getAllJobConfs();
		assertEquals(1, list.size());
		assertEquals(editInfo, list.get(0));
		assertEquals(0, controller.getJobConfs(rootId).size());
		list = controller.getJobConfs(c1);
		assertEquals(1, list.size());
		assertEquals(editInfo, list.get(0));
	}

	public void testUpdateJobConfNullable() throws DaoException {
		int c1 = catalogController.addCatalog(newCataLog(rootId, "c1", null));

		long ct = System.currentTimeMillis();
		JobConf newConf = newCmdJobConf(rootId, "name", "desc", "ping", ct);
		List<ParameterDefine> parameters = new ArrayList<ParameterDefine>();
		parameters.add(new ParameterDefine("p1", ParameterDefineType.none, null));
		parameters.add(new ParameterDefine("p2", ParameterDefineType.pre, "abc"));
		parameters.add(new ParameterDefine("p3", ParameterDefineType.lastDay, "yyyy-MM-dd"));
		newConf.setParameters(parameters);
		Identifier identifier = controller.addJobConf(newConf);

		JobConf editInfo = controller.getJobConf(identifier);
		editInfo.setCatalog(c1);
		editInfo.setName("name3");
		editInfo.setDescription(null);
		((CmdJobConf) editInfo).setCmd(null);
		editInfo.setCreateTime(System.currentTimeMillis());
		editInfo.setParameters(null);
		controller.updateJobConf(editInfo);

		JobConf info = controller.getJobConf(identifier);
		assertEquals(identifier, info.getIdentifier());
		assertEquals(info.getCatalog(), c1);
		assertEquals(info.getType(), CmdJobConf.JOB_TYPE);
		assertEquals(info.getName(), "name3");
		assertNull(info.getDescription());
		assertNull(info.getContent());
		assertNull(info.getParameters());
		assertEquals(info.getCreateTime(), ct);// 创建时间不变
		assertTrue(info.getModifyTime() != null && info.getModifyTime().longValue() > 0
				&& info.getModifyTime().longValue() <= System.currentTimeMillis());// 修改时间自动设置

		editInfo.setCreateTime(ct);
		checkEquals(editInfo, info);

		List<JobConf> list = controller.getAllJobConfs();
		assertEquals(1, list.size());
		assertEquals(editInfo, list.get(0));
		assertEquals(0, controller.getJobConfs(rootId).size());
		list = controller.getJobConfs(c1);
		assertEquals(1, list.size());
		assertEquals(editInfo, list.get(0));
	}

	public void testUpdateJobConfCatalogNotExist() throws DaoException {
		Identifier identifier = controller.addJobConf(newCmdJobConf(rootId, "name", "desc", "ping"));

		JobConf editInfo = controller.getJobConf(identifier);
		editInfo.setCatalog(10000);
		editInfo.setName("name3");
		editInfo.setDescription("desc3");
		((CmdJobConf) editInfo).setCmd("ping2");
		List<ParameterDefine> parameters = new ArrayList<ParameterDefine>();
		parameters.add(new ParameterDefine("p1", ParameterDefineType.none, null));
		parameters.add(new ParameterDefine("p2", ParameterDefineType.pre, "abc"));
		parameters.add(new ParameterDefine("p3", ParameterDefineType.lastDay, "yyyy-MM-dd"));
		editInfo.setParameters(parameters);
		Long mt = System.currentTimeMillis();
		editInfo.setModifyTime(mt);

		try {
			controller.updateJobConf(editInfo);
			fail("应该抛出DaoException");
		} catch (DaoException e) {
			e.printStackTrace();
		}
		assertEquals(editInfo.getModifyTime(), mt);// 传入对象未被更新

		JobConf info = controller.getJobConf(identifier);
		// 对象未被更新
		assertEquals(info.getCatalog(), rootId);
		assertEquals(info.getName(), "name");
		assertEquals(info.getDescription(), "desc");
		assertEquals(info.getContent(), "ping");
		assertNull(info.getParameters());

		List<JobConf> list = controller.getAllJobConfs();
		assertEquals(1, list.size());
		assertEquals(info, list.get(0));
		list = controller.getJobConfs(rootId);
		assertEquals(1, list.size());
		assertEquals(info, list.get(0));
		assertEquals(0, controller.getJobConfs(10000).size());
	}

	public void testUpdateJobConfNameNull() throws DaoException {
		Identifier identifier = controller.addJobConf(newCmdJobConf(rootId, "name", "desc", "ping"));

		JobConf editInfo = controller.getJobConf(identifier);
		editInfo.setName(null);

		try {
			controller.updateJobConf(editInfo);
			fail("应该抛出DaoException");
		} catch (DaoException e) {
			e.printStackTrace();
		}
		assertNull(editInfo.getModifyTime());// 传入对象未被更新

		JobConf info = controller.getJobConf(identifier);
		// 对象未被更新
		assertEquals(info.getCatalog(), rootId);
		assertEquals(info.getName(), "name");
		assertEquals(info.getDescription(), "desc");
		assertEquals(info.getContent(), "ping");
		assertNull(info.getParameters());

		List<JobConf> list = controller.getAllJobConfs();
		assertEquals(1, list.size());
		assertEquals(info, list.get(0));
		list = controller.getJobConfs(rootId);
		assertEquals(1, list.size());
		assertEquals(info, list.get(0));
	}

	public void testUpdateJobConfNameBlank() throws DaoException {
		Identifier identifier = controller.addJobConf(newCmdJobConf(rootId, "name", "desc", "ping"));

		JobConf editInfo = controller.getJobConf(identifier);
		editInfo.setName(" \t ");

		try {
			controller.updateJobConf(editInfo);
			fail("应该抛出DaoException");
		} catch (DaoException e) {
			e.printStackTrace();
		}
		assertNull(editInfo.getModifyTime());// 传入对象未被更新

		JobConf info = controller.getJobConf(identifier);
		// 对象未被更新
		assertEquals(info.getCatalog(), rootId);
		assertEquals(info.getName(), "name");
		assertEquals(info.getDescription(), "desc");
		assertEquals(info.getContent(), "ping");
		assertNull(info.getParameters());

		List<JobConf> list = controller.getAllJobConfs();
		assertEquals(1, list.size());
		assertEquals(info, list.get(0));
		list = controller.getJobConfs(rootId);
		assertEquals(1, list.size());
		assertEquals(info, list.get(0));
	}

	public void testUpdateJobConfIdentifierNull() throws DaoException {
		JobConf conf = newCmdJobConf(rootId, "name", "desc", "ping");
		try {
			controller.updateJobConf(conf);
			fail("应该抛出DaoException");
		} catch (DaoException e) {
			e.printStackTrace();
		}
	}

	public void testUpdateJobConfIdentifierNotExist() throws DaoException, IllegalFactoryException {
		JobConf conf = newCmdJobConf(rootId, "name", "desc", "ping");
		conf.setIdentifier(identifierFactory.createIdentifier(IdentifierType.conf));
		try {
			controller.updateJobConf(conf);
			fail("应该抛出DaoException");
		} catch (DaoException e) {
			e.printStackTrace();
		}
	}

	public void testMoveJobConf() throws DaoException {
		int c1 = catalogController.addCatalog(newCataLog(rootId, "c1", null));

		long ct = System.currentTimeMillis();
		Identifier identifier = controller.addJobConf(newCmdJobConf(rootId, "name", "desc", "ping", ct));

		controller.moveJobConf(identifier, c1);

		JobConf info = controller.getJobConf(identifier);
		assertEquals(identifier, info.getIdentifier());
		assertEquals(info.getCatalog(), c1);
		assertEquals(info.getType(), CmdJobConf.JOB_TYPE);
		assertEquals(info.getName(), "name");
		assertEquals(info.getDescription(), "desc");
		assertEquals(info.getContent(), "ping");
		assertEquals(info.getParameterSize(), 0);
		assertNull(info.getParameters());
		assertEquals(info.getCreateTime(), ct);// 创建时间不变
		assertNull(info.getModifyTime());// 修改时间不变

		List<JobConf> list = controller.getAllJobConfs();
		assertEquals(1, list.size());
		assertEquals(info, list.get(0));
		assertEquals(0, controller.getJobConfs(rootId).size());
		list = controller.getJobConfs(c1);
		assertEquals(1, list.size());
		assertEquals(info, list.get(0));
	}

	public void testMoveJobConfCatalogNotExist() throws DaoException {
		Identifier identifier = controller.addJobConf(newCmdJobConf(rootId, "name", "desc", "ping"));
		try {
			controller.moveJobConf(identifier, 10000);
			fail("应该抛出DaoException");
		} catch (DaoException e) {
			e.printStackTrace();
		}

		JobConf info = controller.getJobConf(identifier);
		// 对象未被更新
		assertEquals(info.getCatalog(), rootId);
		assertEquals(info.getName(), "name");
		assertEquals(info.getDescription(), "desc");
		assertEquals(info.getContent(), "ping");
		assertNull(info.getParameters());

		List<JobConf> list = controller.getAllJobConfs();
		assertEquals(1, list.size());
		assertEquals(info, list.get(0));
		list = controller.getJobConfs(rootId);
		assertEquals(1, list.size());
		assertEquals(info, list.get(0));
		assertEquals(0, controller.getJobConfs(10000).size());
	}

	public void testMoveJobConfIdentifierNull() throws DaoException {
		try {
			controller.moveJobConf(null, rootId);
			fail("应该抛出DaoException");
		} catch (DaoException e) {
			e.printStackTrace();
		}
	}

	public void testMoveJobConfIdentifierNotExist() throws DaoException, IllegalFactoryException {
		try {
			controller.moveJobConf(identifierFactory.createIdentifier(IdentifierType.conf), rootId);
			fail("应该抛出DaoException");
		} catch (DaoException e) {
			e.printStackTrace();
		}
	}

	public void testRemoveJobConf() throws DaoException {
		Identifier c1 = controller.addJobConf(newCmdJobConf(rootId, "name", "desc", "ping"));

		assertEquals(1, controller.getAllJobConfs().size());
		assertEquals(1, controller.getJobConfs(rootId).size());

		controller.removeJobConf(c1);
		assertNull(controller.getJobConf(c1));
		assertEquals(0, controller.getAllJobConfs().size());
		assertEquals(0, controller.getJobConfs(rootId).size());
	}

	public void testRemoveJobConfIdentifierNull() throws DaoException {
		try {
			controller.removeJobConf(null);
			fail("应该抛出DaoException");
		} catch (DaoException e) {
			e.printStackTrace();
		}
	}

	public void testRemoveJobConfIdentifierNotExist() throws DaoException, IllegalFactoryException {
		try {
			controller.removeJobConf(identifierFactory.createIdentifier(IdentifierType.conf));
			fail("应该抛出DaoException");
		} catch (DaoException e) {
			e.printStackTrace();
		}
	}

	public void testRemoveJobConfIdentifierNotConf() throws DaoException, IllegalFactoryException {
		Identifier c1 = controller.addJobConf(newCmdJobConf(rootId, "name", "desc", "ping"));

		try {
			controller.removeJobConf(identifierFactory.createIdentifier(IdentifierType.job));
			fail("应该抛出DaoException");
		} catch (DaoException e) {
			e.printStackTrace();
		}
		assertNotNull(controller.getJobConf(c1));
		assertEquals(1, controller.getAllJobConfs().size());
		assertEquals(1, controller.getJobConfs(rootId).size());
	}

	public void testGetJobConfIdentifierNull() throws DaoException {
		try {
			controller.getJobConf(null);
			fail("应该抛出DaoException");
		} catch (DaoException e) {
			e.printStackTrace();
		}
	}

	public void testGetJobConfIdentifierNotConf() throws DaoException, IllegalFactoryException {
		Identifier c1 = controller.addJobConf(newCmdJobConf(rootId, "name", "desc", "ping"));
		try {
			controller.getJobConf(identifierFactory.createIdentifier(IdentifierType.job));
			fail("应该抛出DaoException");
		} catch (DaoException e) {
			e.printStackTrace();
		}
		assertNotNull(controller.getJobConf(c1));
		assertEquals(1, controller.getAllJobConfs().size());
		assertEquals(1, controller.getJobConfs(rootId).size());
	}

	public void testGetJobConfIdentifierNotExist() throws DaoException, IllegalFactoryException {
		assertNull(controller.getJobConf(identifierFactory.createIdentifier(IdentifierType.conf)));
	}

	public void testRemoveCatalogHasSubConf() throws DaoException {
		int c1 = catalogController.addCatalog(newCataLog(rootId, "c1", "c1"));
		Identifier i1 = controller.addJobConf(newCmdJobConf(c1, "name", "desc", "cmd"));

		try {
			catalogController.removeCatalog(c1);
			fail("应该抛出DaoException");
		} catch (DaoException e) {
			e.printStackTrace();
		}

		assertEquals(2, catalogController.getAllCatalogs().size());
		assertEquals(1, catalogController.getSubCatalogs(rootId).size());
		assertEquals(1, controller.getAllJobConfs().size());
		assertEquals(1, controller.getJobConfs(c1).size());

		controller.removeJobConf(i1);
		catalogController.removeCatalog(c1);

		assertEquals(1, catalogController.getAllCatalogs().size());
		assertEquals(0, catalogController.getSubCatalogs(rootId).size());
		assertEquals(0, controller.getAllJobConfs().size());
		assertEquals(0, controller.getJobConfs(c1).size());
	}
}
